package com.crud.demo1.demos.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crud.demo1.demos.entity.TableUser;
import com.crud.demo1.demos.entity.User;
import com.crud.demo1.demos.Service.ITableUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;

/**
 * @author zhangning
 * @since 2024/4/9 15:42
 */
@Controller
public class ThymeleafController {

    private final ITableUserService iTableUserService;

    @Autowired
    public ThymeleafController(ITableUserService iTableUserService) {
        this.iTableUserService = iTableUserService;
    }

    @RequestMapping("/test")
    public String test(Model model) {
        model.addAttribute("msg", "我是小明，不是小李!");
        return "hello1";
    }

    @RequestMapping("/test2")
    public String test2(Model model) {
        User user = new User();
        user.setAge(21);
        user.setName("Jackson");
        user.setFriend(new User("lixiaolong", 30));
        model.addAttribute("user", user);
        return "hello2";
    }

    @GetMapping("test3")
    public String show3(Model model) {
        model.addAttribute("today", new Date());
        return "hello3";
    }

    /**
     * 分页展示五条数据
     *
     * @param model
     * @return
     */
    @GetMapping("table")
    public String table(Model model) {
        Page<TableUser> page = iTableUserService.page(new Page<>());
        model.addAttribute("users", page.getRecords());
        return "hello5";
    }
}